﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Controls;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace BHan.VM.IndexVM
{
    public partial class PageIndexVM  : ObservableObject,INotifyPropertyChanged
    {


        public PageIndexVM() {
             
        
        }

        [ObservableProperty]
        public RichTextBox richTextContent;

        [RelayCommand]
        private void Open()
        {
            TextRange range;
            FileStream fStream;
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "请选择要加载的文件";
            openFileDialog.Filter = "富文本格式|*.xaml";
            bool? flag = openFileDialog.ShowDialog();
            if (flag == true)
            {
                string _fileName = openFileDialog.FileName;
                if (File.Exists(_fileName))
                {
                    range = new TextRange(this.richTextContent.Document.ContentStart, this.richTextContent.Document.ContentEnd);
                    fStream = new FileStream(_fileName, FileMode.OpenOrCreate);
                    range.Load(fStream, DataFormats.XamlPackage);
                    fStream.Close();
                }
            }
        }

        [RelayCommand]
        private void Save()
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Title = "请选择要保存的路径";
            saveFileDialog.Filter = "富文本格式|*.txt";
            saveFileDialog.FileName = "文档.txt";
            bool? flag = saveFileDialog.ShowDialog();
            if (flag == true)
            {
                string _fileName = saveFileDialog.FileName;
                TextRange range;
                FileStream fStream;
                range = new TextRange(this.richTextContent.Document.ContentStart, this.richTextContent.Document.ContentEnd);
                fStream = new FileStream(_fileName, FileMode.Create);
                range.Save(fStream, DataFormats.Text);
                fStream.Close();
            }
        }

        [RelayCommand]
        private void BgColor(object obj)
        {
            if (obj == null)
            {
                return;
            }
            var item = obj as ComboBoxItem;
            if (item != null)
            {
                var color = item.Background;
                var buttonType = "Background";
                SetColor(buttonType, color);
            }
        }

        [RelayCommand]
        private void ForeColor(object obj)
        {
            if (obj == null)
            {
                return;
            }
            var item = obj as ComboBoxItem;
            if (item != null)
            {
                var color = item.Background;
                var buttonType = "Foreground";
                SetColor(buttonType, color);
            }
        }

        private void SetColor(string buttonType, Brush brush)
        {
            var textSelection = this.richTextContent.Selection;
            if (textSelection == null)
            {
                return;
            }
            if (buttonType == "Background")
            {
                var propertyValue = textSelection.GetPropertyValue(TextElement.BackgroundProperty);
                var bgBrush = (Brush)propertyValue;
                if (bgBrush == brush)
                {
                    textSelection.ApplyPropertyValue(TextElement.BackgroundProperty, Colors.White);
                }
                else
                {
                    textSelection.ApplyPropertyValue(TextElement.BackgroundProperty, brush);
                }
            }
            if (buttonType == "Foreground")
            {
                var propertyValue = textSelection.GetPropertyValue(TextElement.ForegroundProperty);
                var foreground = (Brush)propertyValue;
                if (foreground == brush)
                {
                    textSelection.ApplyPropertyValue(TextElement.ForegroundProperty, Colors.Black);
                }
                else
                {
                    textSelection.ApplyPropertyValue(TextElement.ForegroundProperty, brush);
                }
            }
        }

        [RelayCommand]
        private void Setting(string buttonType)
        {
            var textSelection = this.richTextContent.Selection;
            if (textSelection == null)
            {
                return;
            }
            if (buttonType == "StrikeLine")
            {
                var propertyValue = textSelection.GetPropertyValue(Inline.TextDecorationsProperty);
                var textDecorationCollection = (TextDecorationCollection)propertyValue;
                if (textDecorationCollection == TextDecorations.Strikethrough)
                {
                    textSelection.ApplyPropertyValue(Inline.TextDecorationsProperty, null);
                }
                else
                {
                    textSelection.ApplyPropertyValue(Inline.TextDecorationsProperty, TextDecorations.Strikethrough);
                }
            }
            else if (buttonType == "Super")
            {
                var propertyValue = textSelection.GetPropertyValue(Inline.BaselineAlignmentProperty);
                var supper = (BaselineAlignment)propertyValue;
                if (supper == BaselineAlignment.Superscript)
                {
                    textSelection.ApplyPropertyValue(Inline.BaselineAlignmentProperty, BaselineAlignment.Top);
                }
                else
                {
                    textSelection.ApplyPropertyValue(Inline.BaselineAlignmentProperty, BaselineAlignment.Superscript);
                }
            }
            else if (buttonType == "Sub")
            {
                var propertyValue = textSelection.GetPropertyValue(Inline.BaselineAlignmentProperty);
                var sub = (BaselineAlignment)propertyValue;
                if (sub == BaselineAlignment.Subscript)
                {
                    textSelection.ApplyPropertyValue(Inline.BaselineAlignmentProperty, BaselineAlignment.Top);
                }
                else
                {
                    textSelection.ApplyPropertyValue(Inline.BaselineAlignmentProperty, BaselineAlignment.Subscript);
                }
            }
            else if (buttonType == "Image")
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.Title = "请选择需要插入的图片";
                openFileDialog.Filter = "图片文件|*.png";
                bool? flag = openFileDialog.ShowDialog();
                if (flag == true)
                {
                    var fileName = openFileDialog.FileName;
                    var img = new Image() { Source = new BitmapImage(new Uri(fileName)), Stretch = Stretch.Uniform, Width = this.richTextContent.ActualWidth - 50 };
                    var imgContainer = new BlockUIContainer(img);
                    this.richTextContent.CaretPosition.InsertParagraphBreak();
                    this.richTextContent.Document.Blocks.InsertBefore(this.richTextContent.CaretPosition.Paragraph, imgContainer);
                }
            }
            else if (buttonType == "Print")
            {
                PrintDialog pd = new PrintDialog();
                if ((pd.ShowDialog() == true))
                {
                    //use either one of the below
                    pd.PrintVisual(this.richTextContent as Visual, "打印富文本1");
                    pd.PrintDocument((((IDocumentPaginatorSource)this.richTextContent.Document).DocumentPaginator), "打印富文本2");
                }

                IconElement iconElement = new IconElement();

            }
        }
    }
}
